package com.springboot.project.common.controller;

import com.springboot.config.annotion.BusinessLog;
import com.springboot.config.tool.Tip;
import com.springboot.project.common.entity.UserOnline;
import com.springboot.project.common.service.SessionService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.Collection;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: lby.
 * Date: 2018/7/7.
 * Version: V1.0.
 */
@RequestMapping("/online")
@Controller
public class SessionController {

    @Autowired
    SessionService sessionService;

    @RequestMapping("")
    public String online() {
        return "system/online/online";
    }

    @RequestMapping("/list")
    @ResponseBody
    public List<UserOnline> list() {
        return sessionService.list();
    }

    @ResponseBody
    @BusinessLog(value = "强制下线")
    @RequestMapping("/forceLogout/{sessionId}")
    @RequiresPermissions("/online/forceLogout")
    public Tip forceLogout(@PathVariable("sessionId") String sessionId, RedirectAttributes redirectAttributes) {
        try {
            sessionService.forceLogout(sessionId);
            return new Tip();
        } catch (Exception e) {
            e.printStackTrace();
            return new Tip(500,"系统异常");
        }

    }

    @ResponseBody
    @RequestMapping("/sessionList")
    public Collection<Session> sessionList() {
        return sessionService.sessionList();
    }
}
